package com.zjdiepu.www.biz.third.net;

import java.net.SocketTimeoutException;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.HttpRequestErrorType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.common.consts.ThirdService;
import com.zjdiepu.www.commpent.HttpClient;
import com.zjdiepu.www.config.AliServiceProperty;
import com.zjdiepu.www.ibiz.third.net.IOcrIdcardService;
import com.zjdiepu.www.model.third.request.ali.AliOcrIdcardRequest;
import com.zjdiepu.www.model.third.response.OcrIdcardResponse;
import com.zjdiepu.www.model.third.response.ali.AliOcrIdcardResponse;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 项目名称：dp-biz   
 * 类名称：AliOcrIdcardServiceImpl   
 * 类描述：印刷文字识别_身份证识别：阿里云市场   
 * 创建人：xiongxiaotun
 * 创建时间：2017年3月24日 下午2:14:33   
 * 修改人：xiongxiaotun
 * 修改时间：2017年3月24日 下午2:14:33   
 * 修改备注：   
 * @version V1.0
 */
@Service
public class AliOcrIdcardServiceImpl extends BaseService implements IOcrIdcardService<AliOcrIdcardRequest> {
	private static final Logger logger = LoggerFactory.getLogger(AliOcrIdcardServiceImpl.class);
	@Resource
	protected HttpClient httpClient;
	@Resource
	private AliServiceProperty aliServiceProperty;
	/**
	 * @author xiongxiaotun
	 * @Title ocr 
	 * @Description
	 *   上传图片进行身份证识别：<br/>
	 *   图片最大1.5m <br/>
	 *   把原图片30的压缩率压缩下，超过1m的就压缩<br/>
	 * @return 识别结果 <br/>
	 * @version V1.0
	 * @throws HttpClientException 
	 * @throws SocketTimeoutException 
	 */
	@Override
	public OcrIdcardResponse ocr(AliOcrIdcardRequest ocrIdcardRequest) throws SocketTimeoutException, HttpClientException {
		String responseJson = httpClient.postJson(ThirdService.ALI_MARKET_OCR_IDCARD, JSON.toJSONString(ocrIdcardRequest), getAliCommonHeaderMap(aliServiceProperty.getAppCode()));
		logger.debug(responseJson);
		if(StringUtils.isBlank(responseJson)) {
			ExceptionHelper.thow(HttpRequestErrorType.RESPONSE_IS_EMPTY.getCode(), HttpRequestErrorType.RESPONSE_IS_EMPTY.getValue());
		}
		return JSON.parseObject(responseJson, AliOcrIdcardResponse.class);
	}
}
